package speelotheek.views;

import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.ButtonGroup;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.UIManager;
import javax.swing.JScrollPane;
import javax.swing.ImageIcon;

import speelotheek.models.Overzicht;
import speelotheek.models.Speelgoed;

import java.awt.Color;
import java.awt.Font;
//import java.awt.event.WindowListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Vector;
import javax.swing.ListSelectionModel;

public class Products extends Main 
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -4424306931454281840L;
	private JTextField textField;
	private JTable table;
	private JScrollPane scrollPane;
	private ButtonGroup buttonGroup = new ButtonGroup();
	/**
	 * Create the panel.
	 */
	public Products() 
	{		
		JPanel panelFilter = new JPanel();
		panelFilter.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Filter:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel panelTable = new JPanel();
		panelTable.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "Speelgoed:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel panelButtons = new JPanel();
		panelButtons.setBorder(new TitledBorder(null, "Met Geselecteerd:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel panelMore = new JPanel();
		panelMore.setBorder(new TitledBorder(null, "Meer:", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(panelTable, GroupLayout.DEFAULT_SIZE, 1052, Short.MAX_VALUE)
						.addComponent(panelFilter, GroupLayout.DEFAULT_SIZE, 1052, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(panelButtons, GroupLayout.PREFERRED_SIZE, 579, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(panelMore, GroupLayout.DEFAULT_SIZE, 463, Short.MAX_VALUE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(panelFilter, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(panelTable, GroupLayout.DEFAULT_SIZE, 303, Short.MAX_VALUE)
					.addGap(18)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(panelButtons, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
						.addComponent(panelMore, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		
		JButton btnStatistieken = new JButton("Statistieken");
		btnStatistieken.setIcon(new ImageIcon(Products.class.getResource("/speelotheek/resources/globe_48.png")));
		btnStatistieken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final Statistics stats = new Statistics();
				stats.setVisible(true);
				stats.addWindowListener(new WindowAdapter()
			    {
			      public void windowClosed(WindowEvent e)
			      {
			        stats.removeWindowListener(this);
			      }
			    });
			}
		});
		
		JButton btnSpeelgoedToevoegen = new JButton("Speelgoed Toevoegen");
		btnSpeelgoedToevoegen.setIcon(new ImageIcon(Products.class.getResource("/speelotheek/resources/add_48.png")));
		btnSpeelgoedToevoegen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final ProductInfo productInfo = new ProductInfo();
				productInfo.setVisible(true);
				productInfo.addWindowListener(new WindowAdapter()
			    {
			      public void windowClosed(WindowEvent e)
			      {
			        reloadProducts();
			        productInfo.removeWindowListener(this);
			      }
			    });
			}
		});
		
		GroupLayout gl_panelMore = new GroupLayout(panelMore);
		gl_panelMore.setHorizontalGroup(
			gl_panelMore.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelMore.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnStatistieken)
					.addGap(18)
					.addComponent(btnSpeelgoedToevoegen)
					.addContainerGap(163, Short.MAX_VALUE))
		);
		gl_panelMore.setVerticalGroup(
			gl_panelMore.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelMore.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelMore.createParallelGroup(Alignment.LEADING)
						.addComponent(btnSpeelgoedToevoegen)
						.addComponent(btnStatistieken))
					.addContainerGap(19, Short.MAX_VALUE))
		);
		panelMore.setLayout(gl_panelMore);
					
		JButton btnMeerInfo = new JButton("Wijzigen");
		btnMeerInfo.setIcon(new ImageIcon(Products.class.getResource("/speelotheek/resources/info_48.png")));
		btnMeerInfo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				if (table.getSelectedRow() > -1)
				{
					String code = table.getValueAt(table.getSelectedRow(), 0).toString();
					final ProductInfo productInfo = new ProductInfo(code);
					productInfo.setVisible(true);
					productInfo.removeWindowListener(null);
					productInfo.addWindowListener(new WindowAdapter()
				    {
				      public void windowClosed(WindowEvent e)
				      {
				        reloadProducts();
				        productInfo.removeWindowListener(this);
				      }
				    });
					
				} else {
					JOptionPane.showMessageDialog(null, "Geen speelgoed geselecteerd");
				}
				
			}
		});
		
		JButton btnUitlenen = new JButton("Uitlenen");
		btnUitlenen.setIcon(new ImageIcon(Products.class.getResource("/speelotheek/resources/notepad_48.png")));
		btnUitlenen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRow() > -1)
				{
					String code = table.getValueAt(table.getSelectedRow(), 0).toString();
					final BorrowInfo borrowInfo = new BorrowInfo(code);
					borrowInfo.setVisible(true);
					borrowInfo.addWindowListener(new WindowAdapter()
				    {
				      public void windowClosed(WindowEvent e)
				      {
				        reloadProducts();
				        borrowInfo.removeWindowListener(this);
				      }
				    });
				} else {
					JOptionPane.showMessageDialog(null, "Geen speelgoed geselecteerd");
				}
			}
		});
		
		JButton btnInnemen = new JButton("Innemen");
		btnInnemen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRow() > -1)
				{
					String code = table.getValueAt(table.getSelectedRow(), 0).toString();
					final ReturnInfo returnInfo = new ReturnInfo(code);
					returnInfo.setVisible(true);
					returnInfo.addWindowListener(new WindowAdapter()
				    {
				      public void windowClosed(WindowEvent e)
				      {
				        reloadProducts();
				        returnInfo.removeWindowListener(this);
				      }
				    });
				}
				else
				{
					JOptionPane.showMessageDialog(null, "Geen speelgoed geselecteerd");
				}
			}
		});
		btnInnemen.setIcon(new ImageIcon(Products.class.getResource("/speelotheek/resources/calendar_48.png")));
		
		JButton btnVerwijderen = new JButton("Verwijderen");
		btnVerwijderen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (table.getSelectedRow() > -1){
					String code = table.getValueAt(table.getSelectedRow(), 0).toString();
					
					int confirmRemove = JOptionPane.showConfirmDialog(null, "Weet u zeker dat u dit speelgoed uit de database wilt verwijderen?\n\nLet op: Uitleningen gekoppeld aan dit speelgoed zullen ook verloren gaan.", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
				    
					switch(confirmRemove){
						case JOptionPane.YES_OPTION:
							
							if(Speelgoed.remove(code)){
								reloadProducts();
							} else {
								JOptionPane.showMessageDialog(null, "Er is iets fout gegaan bij het verwijderen van het product. Neem contact op met de beheerder.", "error", JOptionPane.ERROR_MESSAGE);
							}
							
						break;
					}
				} else {
					JOptionPane.showMessageDialog(null, "Geen speelgoed geselecteerd");
				}
			}
		});
		btnVerwijderen.setIcon(new ImageIcon(Products.class.getResource("/speelotheek/resources/cross_48.png")));
		
		GroupLayout gl_panelButtons = new GroupLayout(panelButtons);
		gl_panelButtons.setHorizontalGroup(
			gl_panelButtons.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelButtons.createSequentialGroup()
					.addContainerGap()
					.addComponent(btnMeerInfo)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnVerwijderen)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnUitlenen)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnInnemen)
					.addContainerGap(35, Short.MAX_VALUE))
		);
		gl_panelButtons.setVerticalGroup(
			gl_panelButtons.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelButtons.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelButtons.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnMeerInfo)
						.addComponent(btnVerwijderen, GroupLayout.PREFERRED_SIZE, 57, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnUitlenen)
						.addComponent(btnInnemen))
					.addContainerGap(15, Short.MAX_VALUE))
		);
		panelButtons.setLayout(gl_panelButtons);
		
		JRadioButton rdbtnGeenFilter = new JRadioButton("Geen Filter");
		rdbtnGeenFilter.setSelected(true);
		
		JRadioButton rdbtnUitgeleend = new JRadioButton("Uitgeleend");
		
		JRadioButton rdbtnNietUitgeleend = new JRadioButton("Niet Uitgeleend");
		
		buttonGroup.add(rdbtnGeenFilter);
		buttonGroup.add(rdbtnUitgeleend);
		buttonGroup.add(rdbtnNietUitgeleend);	

		textField = new JTextField();
		textField.setColumns(10);
		
		JButton btnZoeken = new JButton("Zoeken");
		btnZoeken.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Overzicht products = new Overzicht();
				products.setType("toy");			
				products.setFilter(products.getSelectedButtonText(buttonGroup));
				
				String search = textField.getText();
				products.setSearch(search);
				products.createVectors();
				
				Vector data = products.getData();
				Vector columnNames = products.getColumns();
				DefaultTableModel dataModel = new DefaultTableModel(data, columnNames);
				table.setModel(dataModel);
				table.repaint();
			}
		});
		
		JButton btnResetFilter = new JButton("Reset Filter");
		btnResetFilter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				Overzicht products = new Overzicht();
				products.setType("toy");
				products.createVectors();
				
				Vector data = products.getData();
				Vector columnNames = products.getColumns();
				DefaultTableModel dataModel = new DefaultTableModel(data, columnNames);
				table.setModel(dataModel);
				table.repaint();
			}
		});
		
		GroupLayout gl_panelFilter = new GroupLayout(panelFilter);
		gl_panelFilter.setHorizontalGroup(
			gl_panelFilter.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelFilter.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelFilter.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelFilter.createSequentialGroup()
							.addComponent(rdbtnGeenFilter)
							.addGap(18)
							.addComponent(rdbtnUitgeleend)
							.addGap(18)
							.addComponent(rdbtnNietUitgeleend)
							.addPreferredGap(ComponentPlacement.RELATED, 484, Short.MAX_VALUE)
							.addComponent(btnResetFilter))
						.addGroup(gl_panelFilter.createSequentialGroup()
							.addComponent(textField, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnZoeken)))
					.addContainerGap())
		);
		gl_panelFilter.setVerticalGroup(
			gl_panelFilter.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelFilter.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelFilter.createParallelGroup(Alignment.BASELINE)
						.addComponent(rdbtnGeenFilter)
						.addComponent(rdbtnUitgeleend)
						.addComponent(rdbtnNietUitgeleend)
						.addComponent(btnResetFilter))
					.addGap(18)
					.addGroup(gl_panelFilter.createParallelGroup(Alignment.BASELINE)
						.addComponent(textField, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnZoeken))
					.addContainerGap(12, Short.MAX_VALUE))
		);
		panelFilter.setLayout(gl_panelFilter);
		
		setLayout(groupLayout);

		Overzicht products = new Overzicht();
		products.setType("toy");
		products.createVectors();
		
		Vector data = products.getData();
		Vector columnNames = products.getColumns();
		
		table = new JTable(data, columnNames);
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setAutoCreateRowSorter(true);
		table.setRowHeight(20);
		table.setFont(new Font("Tahoma", Font.PLAIN, 12));
		table.setShowVerticalLines(false);
		table.setForeground(Color.BLACK);
		table.setBackground(Color.WHITE);
		scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		GroupLayout gl_panelTable = new GroupLayout(panelTable);
		gl_panelTable.setHorizontalGroup(
			gl_panelTable.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelTable.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 860, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_panelTable.setVerticalGroup(
			gl_panelTable.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelTable.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 345, Short.MAX_VALUE)
					.addContainerGap())
		);
		panelTable.setLayout(gl_panelTable);
	}
	
	public void reloadProducts(){
		
		Overzicht products = new Overzicht();
		products.setType("toy");		
		products.setFilter(products.getSelectedButtonText(buttonGroup));
		
		String search = textField.getText();
		products.setSearch(search);
		
		products.createVectors();
		
		Vector data = products.getData();
		Vector columnNames = products.getColumns();
		DefaultTableModel dataModel = new DefaultTableModel(data, columnNames);
		table.setModel(dataModel);
		table.repaint();
			
	}
}
